//
//  KLMyCenterViewController.m
//  Koala
//
//  Created by 超八机 on 14/11/23.
//  Copyright (c) 2014年 Anonymous. All rights reserved.
//

#import "KLMyMagazineCollectionViewCell.h"
#import "KLMyCenterViewController.h"
#import "KLMagazineModel.h"

@interface KLMyCenterViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *myMagazineCollection;
@property (weak, nonatomic) IBOutlet UIButton *editButton;
@property (strong, nonatomic) NSMutableArray *myMagazineInfoArray;
@property (nonatomic) BOOL showEdit;
@end

@implementation KLMyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    _myMagazineCollection.delegate = self;
    _myMagazineCollection.dataSource = self;
    _myMagazineCollection.backgroundColor = [UIColor clearColor];
    [_myMagazineCollection registerClass:[KLMyMagazineCollectionViewCell class] forCellWithReuseIdentifier:@"KLMyMagazineCollectionViewCell"];
    
    _myMagazineInfoArray = [[NSMutableArray alloc] init];
    _showEdit = NO;
    
    [_myMagazineInfoArray setArray:[self makeTestData]];
    [self addBackButton];
    
}



- (void)addBackButton
{
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(10,15, 44, 44)];
    [leftButton setImage:[UIImage imageNamed:@"btn_back_normal"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    
    [leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)makeTestData
{
    KLMagazineModel *test1 = [[KLMagazineModel alloc] init];
    test1.testCover = [UIImage imageNamed:@"testCover1"];
    
    KLMagazineModel *test2 = [[KLMagazineModel alloc] init];
    test2.testCover = [UIImage imageNamed:@"testCover2"];
    
    KLMagazineModel *test3 = [[KLMagazineModel alloc] init];
    test3.testCover = [UIImage imageNamed:@"testCover3"];
    
    NSArray *dataArray = [NSArray arrayWithObjects:test1,test2,test3,nil];
    
    return dataArray;
}


#pragma mark --UICollectionViewDateSource--
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_myMagazineInfoArray count] + 1;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
 
    static NSString *recommendCollectionViewCellIdentifier = @"KLMyMagazineCollectionViewCell";
    KLMyMagazineCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:recommendCollectionViewCellIdentifier forIndexPath:indexPath];
    if (cell == nil)
    {
 //       cell = [[KLMyMagazineCollectionViewCell alloc] init];
        [collectionView registerNib:[UINib nibWithNibName:@"KLMyMagazineCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:recommendCollectionViewCellIdentifier];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:recommendCollectionViewCellIdentifier forIndexPath:indexPath];
    }
    
    if (indexPath.row < [_myMagazineInfoArray count]) {
        KLMagazineModel *model = [_myMagazineInfoArray objectAtIndex:indexPath.row];
        [cell setMagazine:model.testCover];
        [cell setEditButtonShow:_showEdit];
        [cell setAddButtonShow:NO];
        [cell setCoverBoderShow:YES];
    }else{
        [cell setCoverBoderShow:NO];
        [cell setAddButtonShow:YES];
        [cell setEditButtonShow:NO];
    }

    return cell;
}


#pragma mark  --UICollectionViewDelegateFlowLayout--
//define the cell size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(83, 107);
}

//define the margin
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 15, 15);
}


- (IBAction)collectionButtonClicked:(id)sender {
    
    
}

- (void)setEditButtonText:(NSString *)title
{
    [_editButton setTitle:title forState:UIControlStateNormal];
    [_editButton setTitle:title forState:UIControlStateHighlighted];
}

- (IBAction)modifyButtonClicked:(id)sender {
    if ([_editButton.titleLabel.text isEqualToString:@"编辑"]) {
        //显示编辑按钮
        _showEdit = YES;
        [self setEditButtonText:@"取消"];
        [_myMagazineCollection reloadData];
    }else{
        //隐藏编辑按钮
        _showEdit = NO;
        [self setEditButtonText:@"编辑"];
        [_myMagazineCollection reloadData];
        
    }
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
